// pages/index/shops/shops.js
import {
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    options: ['我的圈子', '所有圈子'],
    type: 0,
    isClickExit: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      theme: app.globalData.theme
    });
    this.shopsLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.isClickExit) {
      app.globalData.pageNumber = 1;
      this.setData({
        result: [],
        isClickExit:false
      });
      this.shopsLoad();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.pageNumber = 1;
    app.globalData.pageTotalNumber = 0;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.globalData.pageNumber = 1;
    this.setData({
      result: []
    });
    this.shopsLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.shopsLoad();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 加载数据
   */
  shopsLoad: function() {
    let params = {};
    if (this.data.type == 0) {
      params["area_code"] = app.globalData.parentAreaCode;
      params["pageNumber"] = app.globalData.pageNumber;
      params["pageSize"] = app.globalData.pageSize;
      params["user_id"] = app.globalData.userInfo["user_id"];
      params["list_type"] = this.data.type;
    } else {
      params["area_code"] = app.globalData.parentAreaCode;
      params["pageNumber"] = app.globalData.pageNumber;
      params["user_id"] = app.globalData.userInfo["user_id"];
      params["pageSize"] = app.globalData.pageSize;
      params["list_type"] = this.data.type;
    }
    app.wxGetRequest("/circle/list", params).then(result => {
      let data = result.data;
      let pageTotalNumber = data.totalPage;
      this.setData({
        result: this.data.result.concat(data.list),
        noData: pageTotalNumber == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = pageTotalNumber;
    });
  },
  /**
   * 商家详情
   */
  indexShopDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/life_service/life_service_detail/life_service_detail?id=' + id,
    })
  },
  /**
   * 切换
   */
  changeType: function(e) {
    let index = e.detail.value;
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
    this.setData({
      result: [],
      type: index
    });
    this.shopsLoad();
  },
  /**
   * 社区加入
   */
  communityOrganizationJoin: function(e) {
    let item = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/index/community_organization/community_organization_join/community_organization_join?item=' + item + "&type=" + this.data.type,
    })
  }
})